#include "PopupItemModel.h"
#include <QDebug>

PopupItemModel::PopupItemModel()
{
}

// 构造父节点下子节点的索引
QModelIndex PopupItemModel::index(int row, int column, const QModelIndex &parent) const
{
    return createIndex(row,column);
}

// 通过子节点索引获取父节点索引
QModelIndex PopupItemModel::parent(const QModelIndex &child) const
{
    return QModelIndex();
}

// 获取父节点下子节点的行数
int PopupItemModel::rowCount(const QModelIndex &parent ) const
{
    return m_data.size();
}

// 获取父节点下子节点列数
int PopupItemModel::columnCount(const QModelIndex &parent) const
{
    return 1;
}

Qt::ItemFlags PopupItemModel::flags(const QModelIndex &index) const
{
    //    return Qt::ItemIsEnabled | Qt::ItemIsEditable;//ItemIsEnable是总开关，还可以使其可被选择等
    return Qt::ItemIsEnabled | Qt::ItemIsEditable;//ItemIsEnable是总开关，还可以使其可被选择等
}

// 获取节点数据：包括DisplayRole|TextAlignmentRole等
QVariant PopupItemModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::TextAlignmentRole)
        return int(Qt::AlignLeft | Qt::AlignVCenter);
    else if (role == Qt::DisplayRole) {
        //                qDebug() << "display";
        return QVariant::fromValue(m_data.at(index.row()));
    }
    else if (role == Qt::EditRole) {
        qDebug() << "edit role";
        return QVariant::fromValue(m_data.at(index.row()));
    } else if (role == Qt::ToolTipRole) {
        return "tips";
    }
    return "know";
}

void PopupItemModel::push(const QPixmap &avatar,const QString &name,const QString &text)
{
    MessageData data;
    data.avatar = avatar;
    data.name = name;
    data.content = text;
    m_data.push_back(data);
    emit dataChanged(createIndex(m_data.size()-1,1),createIndex(m_data.size(),1));
}

void PopupItemModel::updateData(int index,const QString &text)
{
    MessageData &data = m_data[index];
    data.name = text;
    emit dataChanged(createIndex(index-1,1),createIndex(index,1));
}

void PopupItemModel::push(MessageData &data)
{
    if (!data.gifPath.isEmpty()) {
        do {
            QMovie *movie = new QMovie(data.gifPath);
            if (!movie->isValid()) {
                movie->deleteLater();
                break;
            }

            data.ext = movie;
            connect(movie,&QMovie::frameChanged,[this,data,movie](){
                emit dataChanged(createIndex(m_data.size()-1,1),createIndex(m_data.size(),1));
            });
            //            movie->start();
        } while(0);
    }

    m_data.push_back(data);
    if (m_data.size() < 100)
        setGifRange(0,m_data.size());

    emit dataChanged(createIndex(m_data.size()-1,1),createIndex(m_data.size(),1));
}

bool PopupItemModel::removeRow(int row, const QModelIndex &parent) {
    Q_UNUSED(parent);
    if (row > 0 && (row -1) < m_data.size()) {
        int innerRow = row - 1;
        auto &data = m_data[innerRow];
        if (data.ext) {
            data.ext->stop();
            data.ext->deleteLater();
        }

        m_data.removeAt(innerRow);
        emit dataChanged(createIndex(m_data.size()-1,1),createIndex(m_data.size(),1));// 必须，否则不会更新，当移入时会触发ui事件
        return true;
    }
    return false;
}

void PopupItemModel::setGifRange(int begin, int end) {
    // TODO 如果消息超过1万条，会怎样？比如分批暂停

    // 最大句柄问题，每个qmovie是个 QTimer
    for (int i = 0; i < m_data.size(); ++i) {
        if (m_data[i].ext) {
            if (i >= begin && i <= end) {
                m_data[i].ext->setPaused(false);
            } else {
                m_data[i].ext->setPaused(true);
            }
        }
    }
}

